<?php
class UserExt_Bank extends Data_Item
{
    public function __construct ($userId = NULL)
    {
        $this->_uid = (int) $userId;

        $this->_commTableName  = 'mod_bank';
        $this->_commPrimaryKey = 'id';
    }

    public function getBankList ()
    {
        $items = $this->items(array('uid', '=', $this->_uid));
        foreach ($items as & $item)
        {
            $item['accountTypeName'] = $this->getAccountTypeName ($item['accountType']);
        }

        return $items;
    }

    // 设置这条账户为用户的默认银行账户
    public function setDefault ($bankId)
    {
        $this->update(array('isDefault' => 0), array('uid', '=', $this->_uid), NULL);
        $this->update(array('isDefault' => 1), array(
            array('uid', '=', $this->_uid),
            array('id', '=', $bankId)));
    }
    
    public function deleteBank ($bankId)
    {
    	$this->delete(array('id', '=' , $bankId), NULL);   	
    }
    
    public function addBank ($type, $name, $userName)
    {
        $row = array(
            'accountType'     => $type,
            'accountName'     => $name,
            'accountUserName' => $userName,

            'uid'     => $this->_uid,
            'changed' => DB::expr('unix_timestamp(now())'),
            'created' => DB::expr('unix_timestamp(now())'),
        );

        $id = $this->insert($row);
        return $id;
    }

    protected function getAccountTypeName ($type)
    {
        if ($type == 'PAYPAL')
            return 'PAYPAL';
        else
            return '支付宝';
    }
}
